package com.huanzhidadi.single.date;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.TemporalUnit;

public class LocalDateTest {
    public static void main(String[] args) {
        // 从当月开始，往前倒数7个月
        for (int i=0; i<7; i++) {
            LocalDate today = LocalDate.now();
            //System.out.println(today);
            LocalDate localDate = today.minusMonths(i);
            //System.out.println(localDate);
            // 月第一天
            LocalDate firstDay = LocalDate.of(localDate.getYear(), localDate.getMonth(), 1);
            System.out.println(firstDay);
            // 月最后一天
            LocalDate lastDay = localDate.with(TemporalAdjusters.lastDayOfMonth());
            System.out.println(lastDay);
        }

        System.out.println("当天：" + LocalDate.now());
        System.out.println("是否在当天之前：" + LocalDate.now().minusDays(1).isBefore(LocalDate.now()));
        System.out.println("是否在当天之后：" + LocalDate.now().plusDays(1).isAfter(LocalDate.now()));
        System.out.println("是否在当天：" + LocalDate.now().isEqual(LocalDate.now()));
        System.out.println("今年是否是闰年：" + LocalDate.now().isLeapYear());

        // 日期格式化
        String format = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        System.out.println(format);

        // 日期时间格式化
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);
        String format1 = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println(format1);

        // 对日期时间进行小时操作
        LocalDateTime newDate = now.minus(5, ChronoUnit.HOURS);
        System.out.println("减去5小时后，newDate : " + newDate);
        String format2 = newDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println(format2);
    }
}
